package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.AdminDao;
import com.yc.jpyy.mobile.service.AdminService;

@Controller
@RequestMapping(value = "/admin")
public class GetVehicleListInfo extends BaseController {
	
	@Autowired
	private AdminService adminService;

	@Autowired
	private AdminDao adminDao;
	private static Logger logger = LoggerFactory.getLogger(AdminHomePageController.class);
	
	/**
	 * 获取当天车辆信息
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getVehicleListInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getVehicleListInfo(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 驾校Id
			String driverId = request.getParameter("driverId");
			// 车牌号
			String vehicleNo = request.getParameter("vehicleNo");
			if(driverId != null && !driverId.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("driverId", driverId);
				paramsMap.put("vehicleNo", vehicleNo);
				response = adminService.getVehicleListInfo(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填字段！");
			}
		} catch (Exception e) {
			logger.info("getVehicleListInfo:"+ e.toString());
			response.setReturnMsg(0,"获取车辆信息失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	
	/**
	 * 获取车辆轨迹
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 *//*
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getVehicleTraceById", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getVehicleTraceById(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 车辆Id
			String vehicleId = request.getParameter("vehicleId");
			// 开始时间
			String beginTime = request.getParameter("beginTime");
			// 结束时间
			String endTime = request.getParameter("endTime");
			// 时间间隔
			String mergeSeconds = request.getParameter("mergeSeconds");
			// 停车时长
			String stopSeconds = request.getParameter("stopSeconds");
			if(vehicleId != null && !vehicleId.isEmpty()
					&& beginTime != null && !beginTime.isEmpty()
					&& endTime != null && !endTime.isEmpty()){	
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("vehicleId", vehicleId);
				paramsMap.put("beginTime", beginTime);
				paramsMap.put("endTime", endTime);
				if(mergeSeconds != null && !mergeSeconds.isEmpty()){
					paramsMap.put("mergeSeconds", mergeSeconds);
				}else{
					paramsMap.put("mergeSeconds", 0);
				}
				if(stopSeconds != null && !stopSeconds.isEmpty()){
					paramsMap.put("stopSeconds", stopSeconds);
				}else{
					paramsMap.put("stopSeconds", 0);
				}
				response = adminService.getVehicleTraceById(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填字段！");
			}
		} catch (Exception e) {
			logger.info("getVehicleTraceById:"+ e.toString());
			response.setReturnMsg(0,"获取车辆轨迹信息失败！");
		} finally {
			return response.toJSON();
		}
	}*/
	
	
	/**
	 * 获取车辆当前位置
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 *//*
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getVehicleLocation", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getVehicleLocation(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 车辆Id
			String vehicleIds = request.getParameter("vehicleIds");
			if(vehicleIds != null && !vehicleIds.isEmpty()){	
				LocalDate now = LocalDate.now();
				DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy_MM_dd");
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("vehicleIds", vehicleIds.split(","));
				paramsMap.put("tableName", "gps_" + now.format(f));
				response = adminService.getVehicleLocation(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填字段！");
			}
		} catch (Exception e) {
			logger.info("getVehicleLocation:"+ e.toString());
			response.setReturnMsg(0,"获取车辆位置失败！");
		} finally {
			return response.toJSON();
		}
	}*/
}
